package com.home.view.managedbean;

import java.io.Serializable;
import java.security.Principal;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import com.home.model.dto.AccountDto;
import com.home.model.dto.UserDto;
import com.home.model.service.AccountService;
import com.home.model.service.UserService;
import com.home.model.service.impl.AccountServiceImpl;

@ManagedBean(name = "accountBean")
@SessionScoped
public class AccountManagedBean implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private final static Logger LOGGER = Logger.getLogger(AccountManagedBean.class.getName());
	
	@EJB
	private AccountService accountService;
	@EJB
	private UserService userService;
	
	@ManagedProperty(value = "#{languageBean}")
	private LanguageManagedBean languageBean;
	
	private AccountDto accountDto;
	private UserDto userDto;
	private String userName;
	private String password;
	private String language;
	
	/**
	 * process login
	 * @return
	 */
	public String login() {
		FacesContext context = FacesContext.getCurrentInstance();
		HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
		     
		try {
			request.login(this.userName, this.password);
			if (request.isUserInRole("ADMINISTRATOR")) {
				Principal principal = request.getUserPrincipal();
				userDto = userService.findUserByUserName(principal.getName());
				languageBean.countryLocaleCodeChanged(language);
				return "/admin/dashboard.xhtml?faces-redirect=true";
			}
		} catch (Exception e) {
			LOGGER.log(Level.SEVERE, e.getMessage());
		}
		return "login.xhtml";
	}
	
	/**
	 * process logout
	 * @return
	 */
	public String logout() {
		FacesContext context = FacesContext.getCurrentInstance();
		HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
		
		try {
			request.logout();
			
		} catch (Exception e) {
			LOGGER.log(Level.SEVERE, e.getMessage());
		}
		return "login";
	}
	
	public AccountDto getAccountDto() {
		return accountDto;
	}

	public void setAccountDto(AccountDto accountDto) {
		this.accountDto = accountDto;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setAccountService(AccountServiceImpl accountService) {
		this.accountService = accountService;
	}

	public UserDto getUserDto() {
		return userDto;
	}

	public void setUserDto(UserDto userDto) {
		this.userDto = userDto;
	}

	public String getLanguage() {
		return language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}

	public LanguageManagedBean getLanguageBean() {
		return languageBean;
	}

	public void setLanguageBean(LanguageManagedBean languageBean) {
		this.languageBean = languageBean;
	}
	
	
}
